﻿using DemountCIM.Framework;
using DemountCIM.Framework.SECSGEM;
using DemountCIM.ViewHelper;
using DemountCIM.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DemountCIM.View
{
    /// <summary>
    /// MesView.xaml 的交互逻辑
    /// </summary>
    public partial class MesView : UserControl
    {
        System.Timers.Timer dataUpdateTimer;
        MesViewModel model;
        KingEQP EQP;

        _eCommState tempOldCommState;
        _eControlState tempOldControlState;
        public MesView()
        {
            InitializeComponent();
            model = new MesViewModel();
            DataContext = model;

            EQP = KingEQP.GetInstance();
            EQP.AddMessageEvent += model.AddMessage;
            EQP.AddMessage2Event += model.AddMessage2;

            this.btnInitialize.DataContext = GlobalViewModel.GetInstance();
            this.btnInitialize.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsAdmin));
            this.btnStart.DataContext = GlobalViewModel.GetInstance();
            this.btnStart.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsAdmin));
            this.btnStop.DataContext = GlobalViewModel.GetInstance();
            this.btnStop.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsAdmin));
            this.btnClose.DataContext = GlobalViewModel.GetInstance();
            this.btnClose.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsAdmin));

            this.btnOpenCom.DataContext = GlobalViewModel.GetInstance();
            this.btnOpenCom.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsEngineer));
            this.btnCloseCom.DataContext = GlobalViewModel.GetInstance();
            this.btnCloseCom.SetBinding(IsEnabledProperty, nameof(GlobalViewModel.IsEngineer));


            dataUpdateTimer = new System.Timers.Timer();
            dataUpdateTimer.Interval = (double)(1000);  //ms
            dataUpdateTimer.Elapsed += new ElapsedEventHandler(tmr_timeout);
            dataUpdateTimer.Enabled = true;
            dataUpdateTimer.AutoReset = true;      //false:只定时一次, true：重复执行
            dataUpdateTimer.Start();
        }

        void tmr_timeout(object sender, ElapsedEventArgs e)
        {
            var state = EQP.GetCommState();
            if (tempOldCommState != state)
            {
                model.SetCommState(state);
                tempOldCommState = state;
            }

            var controlState = EQP.GetControlState();
            if (tempOldControlState != controlState)
            {
                model.SetControlState(controlState);
                tempOldControlState = controlState;
            }



        }

        private void btnInitialize_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingInitialize(MachineCtrl.GetInstance().MesConfigPath);
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingStart();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingStop();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingClose();
        }

        private void btnOpenCom_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingCommEnable();
        }

        private void btnCloseCom_Click(object sender, RoutedEventArgs e)
        {
            KingEQP.GetInstance().KingCommDisable();
        }


        //testbutton
        private void btnTestCom1_Click(object sender, RoutedEventArgs e)
        {
            //1
            //MachineCtrl.GetInstance().MesControlStateOffline();

            ////2
            //MachineCtrl.GetInstance().MesControlStateLocal();

            ////3
            //MachineCtrl.GetInstance().MesControlStateRemote();

            ////20
            //MachineCtrl.GetInstance().MesEquipmentStateChange(EquipmentState.Undefined);


            ////17
            //MachineCtrl.GetInstance().MesSetAlarm(1);

            ////18
            //MachineCtrl.GetInstance().MesClearAlarm(1);

            ////1601
            //MachineCtrl.GetInstance().MesLogin("Admin", "111111");

            ////1605   
            //MachineCtrl.GetInstance().MesPlateRcmdSuccessful("ID001");

            ////1606 
            //MachineCtrl.GetInstance().MesPlateRcmdFailed("ID001");


            ////2001 
            //MachineCtrl.GetInstance().MesPlateOnloadStation("ID001", 1);
            ////2003
            //MachineCtrl.GetInstance().MesPlateOnloadStation2("ID001");
            ////2002
            //MachineCtrl.GetInstance().MesPlateReadRFID1("ID001");
            ////2004
            //MachineCtrl.GetInstance().MesPlateReadRFID2("ID001");

            ////2005
            //MachineCtrl.GetInstance().MesPlateArriveDemounterStation("ID001", 1);

            ////2006
            //MachineCtrl.GetInstance().MesPlateArriveDemounter2Station("ID001", 1);


            ////2007
            //MachineCtrl.GetInstance().MesPlateLeaveDemounterStation("ID001", 2);

            ////2008
            //MachineCtrl.GetInstance().MesPlateLeaveDemounter2Station("ID001", 2);

            ////2009
            //MachineCtrl.GetInstance().MesPlateArrivePalletizingStation("ID001", 1);

            ////2010
            //MachineCtrl.GetInstance().MesPlateReadRFID3("ID001");

            ////2011
            //MachineCtrl.GetInstance().MesPlateLeavePalletizingStation("ID001", 2);

            ////2012
            //MachineCtrl.GetInstance().MesPlateArriveRemoveGlueStation("ID001", 1);

            ////2013
            //MachineCtrl.GetInstance().MesPlateLeaveRemoveGlueStation("ID001", 1);

            ////2014
            //MachineCtrl.GetInstance().MesPlateArriveCleaningStation("ID001", 1);

            ////2015
            //MachineCtrl.GetInstance().MesPlateLeaveCleaningStation("ID001", 1);

            ////2016
            //MachineCtrl.GetInstance().MesPlateWriteRFID("ID001");

            ////2017
            //MachineCtrl.GetInstance().MesBeamUnloadStation(3, "B", "ID001");

            ////5001
            //MachineCtrl.GetInstance().MesPortTransferStateChange(1, "B", PortTransferState.ReadyToLoad, PortTransferState.ReadyToUnload);

            ////5002
            //MachineCtrl.GetInstance().MesPortAccessModeChange(1, "B", PortAccessMode.ManualMode);

            ////5003
            //MachineCtrl.GetInstance().MesCartLoadComplete(1);

            ////5004
            //MachineCtrl.GetInstance().MesCartUnloadComplete(1);

        }
    }
}
